# -*- coding: utf-8 -*-
# filename: handle.py

import hashlib
import web

class Handle(object):
    def GET(self):
        try:
            data = web.input()
            if len(data) == 0:
                return "hello, this is handle view"
            signature = data.signature
            print(data)
            timestamp = data.timestamp
            print("timestamp是",timestamp,end='\n')
            nonce = data.nonce
            print("nonce:",data.nonce,end='\n')
            echostr = data.echostr
            print("echostr:",echostr,end='\n')
            token = "" #请按照公众平台官网\基本配置中信息填写

            list = [token, timestamp, nonce]
            list.sort()
            sha1 = hashlib.sha1()
            #参考网站 搜索关键词 微信Python2与Python3中hashlib的sha1结果不一致,编码问题

            # https://blog.csdn.net/Ylcacsdn/article/details/101249079
            # https://blog.csdn.net/qq_42798238/article/details/90473360
            #              这是 python2 中的写法   map(sha1.update, list)
            sha1.update("".join(list).encode('utf-8'))  #唯一区别的一句
            hashcode = sha1.hexdigest()
            print("handle/GET func: hashcode, signature: ", hashcode, signature)
            if hashcode == signature:
                print("验证成功")
                return echostr
            else:
                print("验证失败")
                return "验证失败"
        except (ExceptionType) as Argument:
            return Argument